package com.wiken.storeservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wiken.servicecommon.common.Code;
import com.wiken.servicecommon.common.R;
import com.wiken.storeservice.DTO.GeographicLocation;
import com.wiken.storeservice.VO.StoreActivityVO;
import com.wiken.storeservice.entity.StoreActivity;
import com.wiken.storeservice.service.impl.StoreActivityServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wiken
 * @since 2022-12-28
 */
@RestController
@RequestMapping("/storeservice/storeactivity")
public class StoreActivityController {

    @Autowired
    private StoreActivityServiceImpl storeActivityService;
    @PostMapping("getActivityList")
    public R getActivityList(@RequestBody Page page){
        Page<StoreActivity> storeActivityList = storeActivityService.getStoreActivityList(page);

        return R.ok().code(200).data("storeActivityList", storeActivityList);
    }

    @PostMapping("addStoreActivity")
    public R addStoreActivity(@RequestBody StoreActivity storeActivity, @RequestHeader("Authorization") String token){
        boolean success = storeActivityService.addStoreActivity(storeActivity, token);

        if(!success) return R.error().code(Code.ERROR_USER_STORE_ACTIVITY_ADD).message("发布优惠劵失败");

        return R.ok().code(Code.SUCCESS).message("发布优惠劵成功");
    }

    @PostMapping("addStoreActivityByAdministrator")
    public R addStoreActivityByAdministrator(@RequestBody StoreActivity storeActivity, @RequestHeader("Authorization") String token){
        boolean success = storeActivityService.addStoreActivityByAdministrator(storeActivity, token);

        if(!success) return R.error().code(Code.ERROR_USER_STORE_ACTIVITY_ADD).message("发布优惠劵失败");

        return R.ok().code(Code.SUCCESS).message("发布优惠劵成功");
    }

    @PostMapping("getStoreActivityByStoreId")
    public  R getStoreActivityByStoreId(String storeId){
        Page<StoreActivity> storeActivityList = storeActivityService.getStoreActivityByStoreId(storeId);


        return R.ok().code(200).message("获取成功").data("storeActivityList", storeActivityList );
    }

    @PostMapping("getStoreActivityByUserId")
    public R getStoreActivityByUserId(@RequestBody Page page, @RequestHeader("Authorization") String token){
        Page<StoreActivityVO> storeActivityList = storeActivityService.getStoreActivityByUserId(page, token);

        return R.ok().code(200).message("获取优惠劵成功").data("couponList", storeActivityList);
    }

    @PostMapping("getStoreActivityByNearby")
    public R getStoreActivityByNearby(@RequestBody GeographicLocation geographicLocation){
        List<StoreActivityVO> couponList = storeActivityService.getStoreActivityNearby(geographicLocation);

        return R.ok().code(200).message("获取优惠劵成功").data("couponList", couponList);
    }

}
